﻿using AgencyManagerSystem.AsmType;
using AgencyManagerSystem.Common;
using AgencyManagerSystem.Core;
using AgencyManagerSystem.DataFetcher;
using AgencyManagerSystem.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace AgencyManagerSystem.Controllers
{
    public class DimensionController : MyControllerBase
    {
        DimensionManager manager;
        DimensionFetcher fetcher;
        DimensionDocFetcher dFetcher;
        public DimensionController() : base()
        {
            this.manager = new DimensionManager();
            this.fetcher = new DimensionFetcher();
            this.dFetcher = new DimensionDocFetcher();
        }
        [HttpPost]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Super, AppOpt.Create)]
        public AsmMainDetail<DimensionModel, DimensionDocModel> Set( dynamic dataJson)
        {
            string mainStr = (String)dataJson.main;
            string detailStr = (String)dataJson.detail;
            DimensionModel main = Util.ConvertObjectFromJson<DimensionModel>(mainStr);
            IEnumerable<DimensionDocModel> details = Util.ConvertArrayFromJson<DimensionDocModel>(detailStr);
            LogHelper.Debug(this.GetType(), String.Format("SET:[RQS|{2}]{0}|[DETAIL]{1}", Util.ConvertToJson(main), dataJson, RequestIp));
            if (main == null)
            {
                DBHelper.Commit( false);
                LogHelper.Err(this.GetType(), String.Format("SET:从{0}中接收到空的参数", RequestIp));
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                var errResponse = new AsmMainDetail<DimensionModel, DimensionDocModel>() { Message = "传入的参数不能为空", Result = AmsCode.ArgNotAccept };
                LogHelper.Debug(this.GetType(), String.Format("SET:[RSP|{1}]{0}", Util.ConvertToJson(errResponse), RequestIp));
                return errResponse;
            }
            foreach (var item in details)
            {
                item.Dimension = main.AutoId;
            }
            var result = manager.Set(main, details);
            LogHelper.Debug(this.GetType(), String.Format("SET:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            DBHelper.Commit( result.Result);
            return result;
        }
        [HttpPost]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Super, AppOpt.Delete)]
        public AsmObject<bool> Del(DimensionModel dimensionModel)
        {
            LogHelper.Debug(this.GetType(), String.Format("DEL:[RQS|{1}]{0}", Util.ConvertToJson(dimensionModel), RequestIp));
            if (dimensionModel == null)
            {
                DBHelper.Commit( false);
                LogHelper.Err(this.GetType(), String.Format("DEL:从{0}中接收到空的参数", RequestIp));
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return new AsmObject<bool>() { Data = false, Message = "传入的参数不能为空", Result = AmsCode.ArgNotAccept };
            }
            AsmObject<bool> result = manager.Delete(dimensionModel);
            DBHelper.Commit( result.Result);
            LogHelper.Debug(this.GetType(), String.Format("DEL:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
        [HttpPost]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Super, AppOpt.Delete)]
        public AsmArray<Int32> BulkDel(dynamic jsonDimensionModel)
        {
            LogHelper.Debug(this.GetType(), String.Format("BULKDEL:[RQS|{1}]{0}", jsonDimensionModel, RequestIp));
            var models = Util.ConvertArrayFromJson<DimensionModel>((String)jsonDimensionModel.data);
            if (models == null)
            {
                LogHelper.Err(this.GetType(), String.Format("BULKDEL:从{0}中接收到空的参数", RequestIp));
                Response.StatusCode = (Int32)HttpStatusCode.BadRequest;
                var errResponse = new AsmArray<Int32>() { Data = new List<Int32>(), Message = "传入的参数不能为空", Result = AmsCode.ArgNotAccept };
                LogHelper.Debug(this.GetType(), String.Format("BULKDEL:[RSP|{1}]{0}", Util.ConvertToJson(errResponse), RequestIp));
                return errResponse;
            }
            int faild = models.Count();
            var del = new List<Int32>();
            StringBuilder sb = new StringBuilder();
            foreach (var model in models)
            {
                var eff = manager.Delete(model);
                if (eff.Result != 0)
                {
                    sb.AppendFormat("{0}:{1}\r\n", model.AutoId, eff.Message);
                }
                else
                {
                    del.Add(model.AutoId);
                    faild--;
                }
            }
            DBHelper.Commit(true);
            var succResponse = new AsmArray<Int32>() { Data = del, Message = "共有" + (models.Count() - faild) + "笔资料删除成功", Result = AmsCode.Succ };
            LogHelper.Debug(this.GetType(), String.Format("BULKDEL:[RSP|{1}]{0}", Util.ConvertToJson(succResponse), RequestIp));
            return succResponse;
        }
        [HttpGet]
        [Authorize]
        public AsmArray<DimensionModel> List(string jsonCondition, int pos, int size)
        {
            LogHelper.Debug(this.GetType(), String.Format("LIST:[RQS|{1}]{0}", jsonCondition, RequestIp));
            int cnt = 0;
            var ls = fetcher.List(Util.ConvertArrayFromJson<Condition>(jsonCondition), pos, size, out cnt);
            DBHelper.Commit( true);
            var result = new AsmArray<DimensionModel>() { Data = ls, Message = "操作成功", Result = AmsCode.Succ, Cnt = cnt };
            LogHelper.Debug(this.GetType(), String.Format("LIST:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
        [HttpGet]
        [Authorize]
        public AsmMainDetail<DimensionModel, DimensionDocModel> Get(int id)
        {
            LogHelper.Debug(this.GetType(), String.Format("GET:[RQS|{1}]{0}", id, RequestIp));
            var main = fetcher.Get(id);
            if (main == null)
            {
                var errResponse = new AsmMainDetail<DimensionModel, DimensionDocModel> { Data = main, Detail = null, Message = "找不到数据", Result = AmsCode.DataNotFound };
                DBHelper.Commit( false);
                LogHelper.Debug(this.GetType(), String.Format("GET:[RSP|{1}]{0}", Util.ConvertToJson(errResponse), RequestIp));
                return errResponse;
            }
            var detail = dFetcher.ListDetail(id);
            DBHelper.Commit( true);
            var result = new AsmMainDetail<DimensionModel, DimensionDocModel> { Data = main, Detail = detail, Message = "操作成功", Result = AmsCode.Succ };
            LogHelper.Debug(this.GetType(), String.Format("GET:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }

        [HttpGet]
        [Authorize]
        public AsmArray<DimensionDocModel> ListDetail(int id)
        {
            LogHelper.Debug(this.GetType(), String.Format("LISTDETAIL:[RQS|{1}]{0}", id, RequestIp));
            var detail = dFetcher.ListDetail(id);
            DBHelper.Commit(true);
            var result = new AsmArray<DimensionDocModel> {  Data = detail, Message = "操作成功", Result = AmsCode.Succ };
            LogHelper.Debug(this.GetType(), String.Format("LISTDETAIL:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
        [HttpGet]
        [Authorize]
        public AsmArray<DimensionDocModel> ListByProduction(int production,int level)
        {
            LogHelper.Debug(this.GetType(), String.Format("LISTBYPRODUCTION:[RQS|{1}]{0}", production, RequestIp));
            var detail = dFetcher.ListDimensionByProduction(production,level);
            DBHelper.Commit(true);
            var result = new AsmArray<DimensionDocModel> { Data = detail, Message = "操作成功", Result = AmsCode.Succ };
            LogHelper.Debug(this.GetType(), String.Format("LISTBYPRODUCTION:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
        [HttpGet]
        [Authorize]
        public AsmObject<DimensionDocModel> GetDimensionDoc(int id)
        {
            LogHelper.Debug(this.GetType(), String.Format("GETDIMENSIONDOC:[RQS|{1}]{0}", id, RequestIp));
            var detail = dFetcher.Get(id);
            DBHelper.Commit(true);
            var result = new AsmObject<DimensionDocModel> { Data = detail, Message = "操作成功", Result = AmsCode.Succ };
            LogHelper.Debug(this.GetType(), String.Format("GETDIMENSIONDOC:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
    }
}